unit AEWindow; interface uses Memory, QuickDraw, Packages, Menus, Events, Fonts, Scrap, ToolUtils,Resources, Errors, Palettes, LowMem, AppleEvents,AEObjects, AERegistry, Errors, Math,Background,Analysis,Camera,Stacks,Projection, globals, Utilities, Edit, Filters, Graphics, Camera, PlugIns, Macros1, Macros2, File1, File2, Lut, Text, User, AEUtility, Msc; function WindowEvents(var inAppleEvent, reply:AppleEvent; inToken: TokenPtr; theCommand: OSType):OSerr; FUNCTION FindWindowAccessor (desiredClass: DescType; containerToken: AEDesc; containerClass: DescType; keyForm: DescType; keyData: AEDesc; VAR outToken: AEDesc; theRefCon: LongInt): OSErr; function WindowPutData(var inAppleEvent, reply:AppleEvent; inToken: TokenPtr):OSerr; function WindowGetData(var inAppleEvent, reply:AppleEvent; inToken: TokenPtr):OSerr; function GetWindowName(inWindowP:WindowPtr):Str255; implementation type AppleEventPtr = ^AppleEvent; ExtendedPtr = ^Extended; BooleanPtr = ^Boolean; PointPtr = ^Point; function GetAEWindowByPosition(var inAppleEvent:AppleEvent; inKey: AEKeyWord; var outWindowP:WindowPtr):OSErr; VAR theWindowN : longInt; theCount : longInt; theError : OSErr; begin theError := GetAELong(inAppleEvent, inKey, theWindowN); GetAEWindowByPosition := theError; if theError = noErr then begin outWindowP := LMGetWindowList; theCount := 1; while outWindowP <> NIL do begin if theWindowN = theCount then exit(GetAEWindowByPosition); outWindowP := WindowPtr( WindowPeek(outWindowP)^.nextWindow ); theCount := theCount + 1; end; GetAEWindowByPosition := errAENoSuchObject; end end; function GetWindowName(inWindowP:WindowPtr):Str255; VAR theWindowName : str255; theInfo: InfoPtr; kind: integer; begin kind := WindowPeek(inWindowP)^.WindowKind; if kind = PicKind then begin theInfo := pointer(WindowPeek(inWindowP)^.RefCon); theWindowName := theInfo^.title; end else GetWTitle(inWindowP, theWindowName); GetWindowName := theWindowName; end; function GetAEWindowByName(var inAppleEvent:AppleEvent; inKey: AEKeyWord; var outWindowP:WindowPtr):OSErr; VAR theSearchName : str255; theWindowName : str255; theError : OSErr; begin theError := GetAEStr255(inAppleEvent, inKey, theSearchName); GetAEWindowByName := theError; if theError = noErr then begin outWindowP := LMGetWindowList; while outWindowP <> NIL do begin theWindowName := GetWindowName(outWindowP); if IUEqualString(theWindowName, theSearchName) = 0 then exit(GetAEWindowByName); outWindowP := WindowPtr( WindowPeek(outWindowP)^.nextWindow ); end; GetAEWindowByName := errAENoSuchObject; end end; function FindWindowByName(theSearchName: str255; var outWindowP:WindowPtr):OSErr; VAR theWindowName : str255; theError : OSErr; begin outWindowP := LMGetWindowList; while outWindowP <> NIL do begin theWindowName := GetWindowName(outWindowP); if IUEqualString(theWindowName, theSearchName) = 0 then exit(FindWindowByName); outWindowP := WindowPtr( WindowPeek(outWindowP)^.nextWindow ); end; FindWindowByName := errAENoSuchObject; end; function GetAEWindow(var inAppleEvent:AppleEvent; inKey: AEKeyWord; var outWindowP:WindowPtr):OSErr; var theError : OSErr; begin theError := GetAEWindowByName(inAppleEvent, inKey, outWindowP); if theError <> noErr then theError := GetAEWindowByPosition(inAppleEvent, inKey, outWindowP); GetAEWindow := theError; end; { *************************************************************************************** These commands are directed to a window. If a window is not specified a default is used. The AEObject model allows command to be directed to windows that are not foremost or selected. There may be errors in how this is handled as some variables and functions like "CurrentWindow" can noty be switched. *************************************************************************************** } function WindowEvents(var inAppleEvent, reply:AppleEvent; inToken: TokenPtr; theCommand: OSType):OSerr; VAR theError, ignoreErr:OSErr; kind: integer; resultType:DescType; actualSize:Size; theFSS:FSSpec; theWindow:WindowPtr; theName, theString:str255; vRefNum:integer; FinderInfo: FInfo; nToken:NewObjectPtr; answer:Boolean; theEnum:DescType; count:LongInt; begin theWindow := inToken^.containerWindow; theError := noErr; if theCommand = 'cnte' then { COUNT Objects } begin theError := AEGetParamPtr(inAppleEvent, 'kocl', typeType, resultType, Ptr(@theEnum), Sizeof(DescType), actualSize); count := CountObjects(theEnum); theError := AEPutParamPtr(reply, keyDirectObject, typeLongInteger, Ptr(@count), sizeof(LongInt)); end else if theWindow = nil then begin if theCommand = 'open' then { OPEN } begin nToken := NewObjectPtr(inToken); vRefNum := DefaultRefNum; theError := GetFInfo(nToken^.fname, DefaultRefNum, FinderInfo); if theError <> noerr then begin theError := GetVol(nil, vRefNum); theError := GetFInfo(nToken^.fname, vRefNum, FinderInfo); end; if theError = noErr then theError := OpenAFile(nToken^.fname,vRefNum); end end else if theCommand = 'Impo' then { IMPORT } begin nToken := NewObjectPtr(inToken); vRefNum := DefaultRefNum; theError := GetAEStr255(inAppleEvent, 'ImpO', theString); if (theError = noErr) then SetImportOptions(theString); theError := GetFInfo(nToken^.fname, DefaultRefNum, FinderInfo); if theError <> noerr then begin theError := GetVol(nil, vRefNum); theError := GetFInfo(nToken^.fname, vRefNum, FinderInfo); end; if (theError = noErr) then begin answer := ImportFile(nToken^.fname,vRefNum); if answer then theError := errAEEventNotHandled else theError := noErr; end; end else if theCommand = 'clos' then { CLOSE } begin theError := CloseAWindow(theWindow); if theError = 0 then theError := 1 else theError := noErr; end else if theCommand = 'copy' then { COPY } begin FindWhatToCopy; if WhatToCopy <> NothingToCopy then DoCopy; end else if theCommand = 'cut ' then { CUT } begin FindWhatToCopy; if WhatToCopy <> NothingToCopy then DoCut; end else if theCommand = 'open' then SelectWindow(theWindow) { OPEN } else if theCommand = 'past' then { PASTE } begin DoPaste; end else if theCommand = 'prnt' then { PRINT } begin FindWhatToPrint; if WhatToPrint <> NothingToPrint then Print(false); end else if theCommand = 'save' then { SAVE } begin StopDigitizing; if (CurrentWindow = PicKind) or (CurrentWindow = TextKind) or (CurrentWindow = ResultsKind) then theError := GetAEStr255(inAppleEvent, 'ImpO', theString); if (theError = noErr) then SetImportOptions(theString); SaveFile; end else if theCommand = 'slct' then SelectWindow(theWindow) { SELECT } else theError := errAEEventNotHandled; WindowEvents := theError; end; FUNCTION FindWindowAccessor (desiredClass: DescType; containerToken: AEDesc; containerClass: DescType; keyForm: DescType; keyData: AEDesc; VAR outToken: AEDesc; theRefCon: LongInt): OSErr; VAR theError: OSErr; theName: Str255; actSize: Size; windTitle: Str255; theWindow: WindowPtr; iLoop,index: integer; found: Boolean; kind: integer; theID: LongInt; theInfo: InfoPtr; xval: extended; BEGIN theWindow := nil; IF keyForm = formName THEN BEGIN if (keyData.descriptorType = 'long') or (keyData.descriptorType = 'shor') then begin theError := LongFromDesc(keyData, theID); theName := StringOf(theID:3); for iLoop := 1 to length(theName) do if theName[iLoop] = ' ' then theName[iLoop] := '0'; end else if keyData.descriptorType = 'doub' then begin theError := ExtendedFromDesc(keyData, xval); RealToString(xval,4,1, theName); for iLoop := 1 to length(theName) do if theName[iLoop] = ' ' then theName[iLoop] := '0'; end else theError := StringFromDesc(keyData, theName); theError := FindWindowByName(theName, theWindow); if theWindow = nil then begin MakeNewTokenDesc(theName, 'cwin', outToken); FindWindowAccessor := noErr; exit(FindWindowAccessor); end; END ELSE IF keyForm = formAbsolutePosition THEN BEGIN theError := ShortFromDesc(keyData, index); found := FALSE; iLoop := 0; theWindow := FrontWindow; gObjIndex := index; WHILE (theWindow <> NIL) AND (found <> TRUE) DO BEGIN iLoop := iLoop +1; IF iLoop = index THEN found := TRUE ELSE theWindow := WindowPtr(WindowPeek(theWindow)^.nextWindow); END; {of while} END {of formAbsolutePosition} {handle the other key forms you support} ELSE BEGIN FindWindowAccessor := errAEBadKeyForm; Exit(FindWindowAccessor); END; IF theWindow = NIL THEN begin FindWindowAccessor := errAENoSuchObject; end ELSE {create token that identifies the window} BEGIN kind := WindowPeek(theWindow)^.WindowKind; if kind = PicKind then begin theInfo := pointer(WindowPeek(theWindow)^.RefCon); if theInfo^.StackInfo <> nil then MakeTokenDesc(theWindow, 'cwin', outToken) else MakeTokenDesc(theWindow, 'Ipic', outToken); end else MakeTokenDesc(theWindow, 'cwin', outToken); FindWindowAccessor := noErr; end END; function WindowPutData(var inAppleEvent, reply:AppleEvent; inToken: TokenPtr):OSerr; VAR theError:OSErr; theValueL:LongInt; theValueX:extended; theState : boolean; theName:str255; thePoint:Point; theRect:Rect; theAEProp: AEDesc; theWindow: WindowPtr; kind, theValue: integer; SavePort: GrafPtr; pt:Point; begin WindowPutData := errAEEventNotHandled; if inToken^.containerWindow <> nil then with inToken^ do begin theWindow := containerWindow; kind := WindowPeek(theWindow)^.WindowKind; theRect := theWindow^.portRect; pt.h := theRect.left; pt.v := theRect.top; GlobalToLocal(pt); if name = 'pnam' then begin theError := AEGetParamDesc(inAppleEvent, 'data', 'TEXT', theAEProp); theError := StringFromDesc(theAEProp, theName); SetWTitle(theWindow, theName); UpdateWindowsMenuItem; end else if name = 'hite' then begin theValue := GetShortProperty(inAppleEvent,10,10000); SizeWindow(theWindow, theValue, theRect.bottom - theRect.top + theValue, true); end else if name = 'wide' then begin theValue := GetShortProperty(inAppleEvent,10,10000); SizeWindow(theWindow, theRect.right - theRect.left + theValue, theValue, true); end else if name = 'pbnd' then begin theRect := GetRectProperty(inAppleEvent); SizeWindow(theWindow, theRect.left - theRect.right, theRect.bottom - theRect.top, true); MoveWindow(theWindow, theRect.left, theRect.top, false); end else if name = 'pLft' then begin theValue := GetShortProperty(inAppleEvent,-10000,10000); MoveWindow(theWindow, theValue, pt.v, false); end else if name = 'pTop' then begin theValue := GetShortProperty(inAppleEvent,-10000,10000); MoveWindow(theWindow, pt.h, theValue, false); end else if name = 'pLoc' then begin pt := GetPointProperty(inAppleEvent); MoveWindow(theWindow, pt.h, pt.v, false); end end; WindowPutData := theError; end; function WindowGetData(var inAppleEvent, reply:AppleEvent; inToken: TokenPtr):OSerr; VAR theError:OSErr; theValueL:LongInt; theValueX:extended; theState : boolean; theName:str255; pt:Point; theRect:Rect; theWindow: WindowPtr; kind: integer; SavePort: GrafPtr; theKind: DescType; begin with inToken^ do begin theWindow := containerWindow; GetPort(SavePort); SetPort(theWindow); kind := WindowPeek(theWindow)^.WindowKind; theRect := theWindow^.portRect; pt.h := 0; pt.v := 0; LocalToGlobal(pt); if name = 'pnam' then begin theName := GetWindowName(theWindow); theError := AEPutParamPtr(reply, keyDirectObject, 'TEXT', Ptr(@theName[1]), length(theName)); end else if name = 'kind' then begin theValueL := kind - PicKind; theKind := LongToEnum(theValueL,200); theError := AEPutParamPtr(reply, keyDirectObject, typeEnumerated, Ptr(@theKind), sizeof(DescType)); end else if name = 'wide' then theError := ReturnShortProperty(reply, theRect.right - theRect.left) else if name = 'hite' then theError := ReturnShortProperty(reply, theRect.bottom - theRect.top) else if name = 'pLoc' then theError := ReturnPointProperty(reply, pt) else if name = 'pbnd' then begin OffsetRect(theRect, pt.h, pt.v); theError := AEPutParamPtr(reply, keyDirectObject, typeQDRectangle, @theRect, sizeof(theRect)); end else if name = 'pLft' then theError := ReturnShortProperty(reply,pt.h) else if name = 'pTop' then theError := ReturnShortProperty(reply,pt.v) end; WindowGetData := theError; end; end.